package edu.snake.implement.model.character.iterator;

import edu.snake.core.model.character.Character;

import java.util.Iterator;
import java.util.Stack;


public class CompositeIterator implements Iterator<Character> {

    private final Stack<Iterator<Character>> stack = new Stack<Iterator<Character>>();

    public CompositeIterator(Iterator<Character> iterator) {
        stack.push(iterator);
    }

    public Character next() {
        if (hasNext()) {
            Iterator iterator = stack.peek();
            Character character = (Character) iterator.next();
            if (character.iterator() != null) {

                stack.push(character.iterator());
            }
            return character;
        } else {
            return null;
        }
    }

    public boolean hasNext() {
        if (stack.empty()) {
            return false;
        } else {
            Iterator iterator = stack.peek();
            if (!iterator.hasNext()) {
                stack.pop();
                return hasNext();
            } else {
                return true;
            }
        }
    }
}
